#include "Timer.h"

namespace GeorgeLib
{

	double	Timer::PerformanceCounterFreq = 0;

	Timer::Timer(void)
	{
		LARGE_INTEGER temp;
		if (!QueryPerformanceFrequency(&temp))
			Logger->AddLog("Timer\t\t: Constructor - QueryPerformanceFrequency() failed");

		PerformanceCounterFreq = double(temp.QuadPart) / 1000000.0;
	}

	Timer::~Timer(void)
	{
	}

	int Timer::GetTime(void)
	{
		return timeGetTime();
	}

	void Timer::StartCounter(int & prevTime)
	{
		prevTime = timeGetTime();
	}

	int Timer::GetCounter(int prevTime)
	{
		return timeGetTime() - prevTime;
	}

	double Timer::GetTimePrecise(void)
	{
		LARGE_INTEGER time;
		QueryPerformanceCounter(&time);

		return time.QuadPart / PerformanceCounterFreq;
	}

	void Timer::StartCounterPrecise(double & prevTime)
	{
		prevTime = GetTimePrecise();
	}

	double Timer::GetCounterPrecise(double prevTime)
	{
		return GetTimePrecise() - prevTime;
	}

	bool Timer::GetDelay(const int delay, int & prevTime)
	{
		if (prevTime == 0) prevTime = timeGetTime();	// Reset/initialise prevTime
		int currTime = timeGetTime();				// Initialise currTime

		if (currTime - prevTime > delay)
		{
			prevTime = 0;
			return true;
		}

		return false;
	}

	bool Timer::GetDelayPrecise(const double delay, double & prevTime)
	{
		if (prevTime == 0) prevTime = GetTimePrecise();// Reset/initialise prevTime
		double currTime = GetTimePrecise();			// Initialise currTime

		if (currTime - prevTime > delay)
		{
			prevTime = 0;
			return true;
		}

		return false;
	}

} // namespace GeorgeLib